package com.hebada.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

/**
 * Created by paddy.luo on 2017/10/11.
 */
public class HtmlUtils {

  private static final String REG_EX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
  private static final String REG_EX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
  private static final String REG_EX_HTML = "<[^>]+>";

  public static String filterHtmlTag(String content) {
    if (!StringUtils.hasLength(content)) return null;

    // filter script tag
    Pattern scriptPattern = Pattern.compile(REG_EX_SCRIPT, Pattern.CASE_INSENSITIVE);
    Matcher matcher = scriptPattern.matcher(content);
    content = matcher.replaceAll("");

    // filter style tag
    Pattern stylePattern = Pattern.compile(REG_EX_STYLE, Pattern.CASE_INSENSITIVE);
    matcher = stylePattern.matcher(content);
    content = matcher.replaceAll("");

    // filter html tag
    Pattern p_html = Pattern.compile(REG_EX_HTML, Pattern.CASE_INSENSITIVE);
    Matcher m_html = p_html.matcher(content);
    content = m_html.replaceAll("");

    return content;
  }

  private HtmlUtils() {
  }
}
